{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6a5c68ff",
   "metadata": {},
   "source": [
    "# import packages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "632a4a7b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pyutils.filename as fn\n",
    "import pyutils.ctutils.driver as ctd\n",
    "import STmodel.data.cases as stc"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "65769975",
   "metadata": {},
   "source": [
    "# load the case"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b5abbcb0",
   "metadata": {},
   "outputs": [],
   "source": [
    "cs = stc.case.CaseSet('Lu_2021')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "07ebd294",
   "metadata": {},
   "source": [
    "## get chemical kinetic model, fuel and oxidizer stream composition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "973d32e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "mech = cs.get_mechanism()\n",
    "fuel = cs.get_fuel_stream()\n",
    "oxys = cs.get_oxy_stream()\n",
    "\n",
    "## alternatively, user can specify these info manually as\n",
    "#mech = 'hydrogen_Burke_N2.cti'\n",
    "#fuel = {'H2':1.0}\n",
    "#oxys = {'O2':1.0, 'N2':3.76}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1e103e16",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/Users/zhen/opt/pyutils/ctutils/mechanisms/hydrogen_Burke_N2.cti'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mech"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2a68a8a2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'H2'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fuel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "669c6920",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'O2': 1.0, 'N2': 3.76}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "oxys"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "065f8280",
   "metadata": {},
   "source": [
    "## get the T, p, phi conditions in the case set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "77e99442",
   "metadata": {},
   "outputs": [],
   "source": [
    "cases = cs.cases"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "245c87ca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['T-300_p-10_phi-0.6']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cases"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7df7ed9",
   "metadata": {},
   "source": [
    "## alternatively, user can specify these info manually as"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "c8951492",
   "metadata": {},
   "outputs": [],
   "source": [
    "cases = ['T-300_p-10_phi-0.6',]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c2c9a79",
   "metadata": {},
   "source": [
    "# run 1D simulations for the corresponding conditions\n",
    "it will generate a folder containing flame solutions for each condition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "30234464",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Solution saved to file T-300_p-10_phi-0.6.xml as solution solution.\n",
      "Strain rate = 1000 initialization\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-1000.xml as solution solution.\n",
      "Strain rate = 1000 success\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/zhen/anaconda3/envs/ct/lib/python3.7/site-packages/cantera/onedim.py:311: DeprecationWarning: Property 'u' to be removed after Cantera 2.5. Replaced by property 'velocity'\n",
      "  DeprecationWarning)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DeprecationWarning: StFlow::componentIndex: To be changed after Cantera 2.5. Solution component 'u' renamed to 'velocity'\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/zhen/anaconda3/envs/ct/lib/python3.7/site-packages/cantera/onedim.py:333: DeprecationWarning: Property 'V' to be removed after Cantera 2.5. Replaced by property 'spread_rate'\n",
      "  DeprecationWarning)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Solution saved to file T-300_p-10_phi-0.6_a-1105.17.xml as solution solution.\n",
      "Strain rate = 1105.17 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-1221.4.xml as solution solution.\n",
      "Strain rate = 1221.4 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-1349.86.xml as solution solution.\n",
      "Strain rate = 1349.86 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-1491.82.xml as solution solution.\n",
      "Strain rate = 1491.82 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-1648.72.xml as solution solution.\n",
      "Strain rate = 1648.72 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-1822.12.xml as solution solution.\n",
      "Strain rate = 1822.12 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-2013.75.xml as solution solution.\n",
      "Strain rate = 2013.75 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-2225.54.xml as solution solution.\n",
      "Strain rate = 2225.54 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-2459.6.xml as solution solution.\n",
      "Strain rate = 2459.6 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-2718.28.xml as solution solution.\n",
      "Strain rate = 2718.28 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-3004.17.xml as solution solution.\n",
      "Strain rate = 3004.17 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-3320.12.xml as solution solution.\n",
      "Strain rate = 3320.12 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-3669.3.xml as solution solution.\n",
      "Strain rate = 3669.3 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-4055.2.xml as solution solution.\n",
      "Strain rate = 4055.2 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-4481.69.xml as solution solution.\n",
      "Strain rate = 4481.69 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-4953.03.xml as solution solution.\n",
      "Strain rate = 4953.03 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-5473.95.xml as solution solution.\n",
      "Strain rate = 5473.95 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-6049.65.xml as solution solution.\n",
      "Strain rate = 6049.65 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-6685.89.xml as solution solution.\n",
      "Strain rate = 6685.89 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-7389.06.xml as solution solution.\n",
      "Strain rate = 7389.06 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-8166.17.xml as solution solution.\n",
      "Strain rate = 8166.17 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-9025.01.xml as solution solution.\n",
      "Strain rate = 9025.01 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-9974.18.xml as solution solution.\n",
      "Strain rate = 9974.18 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-11023.2.xml as solution solution.\n",
      "Strain rate = 11023.2 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-12182.5.xml as solution solution.\n",
      "Strain rate = 12182.5 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-13463.7.xml as solution solution.\n",
      "Strain rate = 13463.7 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-14879.7.xml as solution solution.\n",
      "Strain rate = 14879.7 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-16444.6.xml as solution solution.\n",
      "Strain rate = 16444.6 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-18174.1.xml as solution solution.\n",
      "Strain rate = 18174.1 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-20085.5.xml as solution solution.\n",
      "Strain rate = 20085.5 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-22198.xml as solution solution.\n",
      "Strain rate = 22198 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-24532.5.xml as solution solution.\n",
      "Strain rate = 24532.5 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-27112.6.xml as solution solution.\n",
      "Strain rate = 27112.6 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-29964.1.xml as solution solution.\n",
      "Strain rate = 29964.1 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-33115.5.xml as solution solution.\n",
      "Strain rate = 33115.5 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-36598.2.xml as solution solution.\n",
      "Strain rate = 36598.2 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-40447.3.xml as solution solution.\n",
      "Strain rate = 40447.3 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-44701.2.xml as solution solution.\n",
      "Strain rate = 44701.2 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-49402.4.xml as solution solution.\n",
      "Strain rate = 49402.4 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-54598.2.xml as solution solution.\n",
      "Strain rate = 54598.2 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-60340.3.xml as solution solution.\n",
      "Strain rate = 60340.3 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-66686.3.xml as solution solution.\n",
      "Strain rate = 66686.3 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-73699.8.xml as solution solution.\n",
      "Strain rate = 73699.8 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-81450.9.xml as solution solution.\n",
      "Strain rate = 81450.9 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-90017.1.xml as solution solution.\n",
      "Strain rate = 90017.1 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-99484.3.xml as solution solution.\n",
      "Strain rate = 99484.3 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-109947.xml as solution solution.\n",
      "Strain rate = 109947 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-121510.xml as solution solution.\n",
      "Strain rate = 121510 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-134290.xml as solution solution.\n",
      "Strain rate = 134290 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-148413.xml as solution solution.\n",
      "Strain rate = 148413 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-164022.xml as solution solution.\n",
      "Strain rate = 164022 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-181272.xml as solution solution.\n",
      "Strain rate = 181272 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-200337.xml as solution solution.\n",
      "Strain rate = 200337 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-221406.xml as solution solution.\n",
      "Strain rate = 221406 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-244692.xml as solution solution.\n",
      "Strain rate = 244692 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-270426.xml as solution solution.\n",
      "Strain rate = 270426 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-298867.xml as solution solution.\n",
      "Strain rate = 298867 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-330300.xml as solution solution.\n",
      "Strain rate = 330300 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-365037.xml as solution solution.\n",
      "Strain rate = 365037 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-403429.xml as solution solution.\n",
      "Strain rate = 403429 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-445858.xml as solution solution.\n",
      "Strain rate = 445858 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-492749.xml as solution solution.\n",
      "Strain rate = 492749 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-544572.xml as solution solution.\n",
      "Strain rate = 544572 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-601845.xml as solution solution.\n",
      "Strain rate = 601845 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-665142.xml as solution solution.\n",
      "Strain rate = 665142 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-735095.xml as solution solution.\n",
      "Strain rate = 735095 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-812406.xml as solution solution.\n",
      "Strain rate = 812406 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-897847.xml as solution solution.\n",
      "Strain rate = 897847 success\n",
      "Solution saved to file T-300_p-10_phi-0.6_a-992275.xml as solution solution.\n",
      "Strain rate = 992275 success\n"
     ]
    }
   ],
   "source": [
    "for condition in cases:\n",
    "    \n",
    "    # get unburnt status T, p, phi from condition string\n",
    "    para = fn.name2params(condition)\n",
    "    T = para['T']\n",
    "    p = para['p']\n",
    "    phi = para['phi']\n",
    "    \n",
    "    # calculate freely propagating flame and \n",
    "    # counterflow flames from weak stretch to extinction\n",
    "    ctd.counterflow_premixed_extinction(\n",
    "        chemistry=mech,             # chemical kinetic model\n",
    "        fuel=fuel, oxidizer=oxys,   # fuel and oxidizer stream\n",
    "        T=T, p=p, phi=phi,          # unburnt status T, p, phi\n",
    "        ct_ratio=2.0, ct_slope=0.1, # numerical settings for flame solver\n",
    "        ct_curve=0.1, ct_prune=0.05,# check cantera for settings\n",
    "        a_init=1000.,               # mean strain rate for the first point of the counterflow simulations\n",
    "        a_max=1e+6,                 # max mean strain rate for counterflow simulation\n",
    "        L_init=0.005                # domain length for the first point of the counterflow simulations\n",
    "        )"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:ct] *",
   "language": "python",
   "name": "conda-env-ct-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
